package net.anzhuo.controller.db.report;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.entity.PaginationVO;
import net.anzhuo.common.utils.RedisUtil;
import net.anzhuo.entity.GsBase;
import net.anzhuo.entity.GsShangbiao;
import net.anzhuo.entity.db.shangbiao.Paging;
import net.anzhuo.entity.db.shangbiao.ResponseResult;
import net.anzhuo.entity.db.shangbiao.Result;
import net.anzhuo.service.GsBaseService;
import net.anzhuo.service.GsShangbiaoService;

@RestController
@Slf4j
public class ShangbiaoController {

	@Autowired
	private GsShangbiaoService gsShangbiaoService;
	@Autowired
	private GsBaseService gsBaseService;
	///report/shangbiao
	@RequestMapping("/tm/Search")
	public ResponseResult getShangbiao(String keyword,String searchType,String intCls,String pageIndex, String pageSize) {
		log.info("===>tmSearch_keyword="+keyword+", pageSize="+pageSize);
		long startTime=System.currentTimeMillis();
		if (StringUtils.isBlank(pageSize)) {
			pageSize = "50";
		}
		
		ResponseResult rr = null;
		try {
			rr = (ResponseResult)RedisUtil.get("tmSearch:"+keyword);
			if (rr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" tmSearch 执行时间："+excTime+"s");
				return rr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		GsShangbiao gsShangbiaoQuery = new GsShangbiao();
		GsBase gsBase=gsBaseService.getByMd5(keyword);
		gsShangbiaoQuery.setCoMd5(keyword);
		gsShangbiaoQuery.setPageSize(Integer.parseInt(pageSize));
		PaginationVO<GsShangbiao> shangbiaoList = gsShangbiaoService.getListWithPage(gsShangbiaoQuery);
		
		ResponseResult responseResult = new ResponseResult();
		responseResult.setStatus("200");
		responseResult.setMessage("查询成功");
		
		Paging paging = new Paging();
		paging.setPageIndex(1);
		paging.setPageSize(Integer.parseInt(pageSize));
		paging.setTotalRecords(shangbiaoList.getTotalCount());
		responseResult.setPaging(paging);
		
		List<Result> resultList = new ArrayList<>();
		Result result = null;
		if (shangbiaoList.getList()!=null && shangbiaoList.getList().size()>0) {
			for (GsShangbiao s : shangbiaoList.getList()) {
				result = new Result();
				result.setRegNo(s.getRegNumber());
				result.setName(s.getTrademarkName());
				result.setAppDate(s.getApplyTime());
				result.setAgent(s.getAgent());
				result.setApplicantCn(s.getApplicantCn());
				result.setApplicantEn(s.getApplicantEn());
				result.setCls(s.getType());
				result.setId(s.getMd5());
				result.setName(s.getTrademarkName());
				result.setStatus(s.getState());
				result.setHasImage(s.getHasImage());
				result.setFlowStatus(s.getFlowStatus());
				result.setFlowStatusDesc(s.getFlowStatusDesc());
				result.setImageUrl(s.getTrademark());
				resultList.add(result);
			}
		}
		responseResult.setResult(resultList);
		
		try {
			RedisUtil.set("tmSearch:"+keyword, responseResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" tmSearch 执行时间："+excTime+"s");
		return responseResult;
	}
}
